package com.jihox.abookpro.fragment;

import com.jihox.abookpro.R;
import com.jihox.abookpro.view.MaskView;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

@SuppressLint("ValidFragment")
public class AssertFragment extends Fragment {

	private int flag;
	private Rect rect;
	private int bottom;
	private int right;
	private int cx =0;
	private int cy = 0;
	private int radius =0;

	public AssertFragment() {
	}

	public AssertFragment(int flag) {
		this.flag = flag;
	}
	
	public AssertFragment(int flag,Rect rect,int bottom) {
		this.flag = flag;
		this.rect=rect;
		this.bottom=bottom;
	}
	
	public AssertFragment(int flag,Rect rect,int bottom,int right) {
		this.flag = flag;
		this.rect=rect;
		this.bottom=bottom;
		this.right=right;
	}
	
	public AssertFragment(int flag,int cx,int cy,int radius) {
		this.flag = flag;
		this.cx = cx;
		this.cy = cy;
		this.radius = radius;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		DisplayMetrics metric = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metric);
		View view = null;
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		switch (flag) {
		case 1:
			view = inflater.inflate(R.layout.layout_assert_showimage,
					container, false);
			TextView textDeleteTip = (TextView) view.findViewById(R.id.txthint);
			textDeleteTip.setText(R.string.delete_tip);
			RelativeLayout layoutHint=(RelativeLayout) view.findViewById(R.id.layout_hint);
			layoutHint.setPadding(layoutHint.getPaddingLeft(), layoutHint.getPaddingTop(), layoutHint.getPaddingRight(), layoutHint.getPaddingBottom()+bottom);
			MaskView maskView =(MaskView) view.findViewById(R.id.guide_view);
			maskView.setCenterRect(rect);
			break;
		case 2:
			view = inflater.inflate(R.layout.layout_assert_curl,
					container, false);
			TextView textEditTip = (TextView) view.findViewById(R.id.txthint);
			textEditTip.setText(R.string.edit_tip);
			layoutHint=(RelativeLayout) view.findViewById(R.id.layout_hint);
			layoutHint.setPadding(layoutHint.getPaddingLeft(), layoutHint.getPaddingTop(), layoutHint.getPaddingRight()+right, layoutHint.getPaddingBottom()+bottom);
			maskView =(MaskView) view.findViewById(R.id.guide_view);
			//maskView.setCenterRect(rect);
			break;
		case 3:
			view = inflater.inflate(R.layout.layout_assert_showimage,
					container, false);
			layoutHint=(RelativeLayout) view.findViewById(R.id.layout_hint);
			TextView text = (TextView)view.findViewById(R.id.txthint);
			text.setText(R.string.change_sequence_tip);
			layoutHint.setPadding(layoutHint.getPaddingLeft(), layoutHint.getPaddingTop(), layoutHint.getPaddingRight(), layoutHint.getPaddingBottom()+cy+radius);
			maskView =(MaskView) view.findViewById(R.id.guide_view);
			maskView.setCircle(cx, cy, radius);
			break;
		case 4:
			view = inflater.inflate(R.layout.layout_assert_curl,
					container, false);
			TextView textShareTip = (TextView)view.findViewById(R.id.txthint);
			textShareTip.setText(R.string.share_tip);
			layoutHint=(RelativeLayout) view.findViewById(R.id.layout_hint);
			int	marginBottom = metric.heightPixels-(layoutHint.getPaddingBottom()+cy+radius)-60;
			layoutHint.setPadding(layoutHint.getPaddingLeft(), layoutHint.getPaddingTop(), layoutHint.getPaddingRight(), marginBottom);
			maskView =(MaskView) view.findViewById(R.id.guide_view);
			maskView.setCircle(cx, cy, radius);
			break;

		default:
			view = inflater.inflate(R.layout.layout_assert_showimage,
					container, false);
			break;

		}
		view.setLayoutParams(params);
		view.findViewById(R.id.txthint).bringToFront();
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SharedPreferences preferences = getActivity()
						.getSharedPreferences("assert_pref",
								Context.MODE_APPEND);
				Editor editor = preferences.edit();
				switch (flag) {
				case 1:
					editor.putBoolean("isShowImageNeedAssert", false);
					break;
				case 2:
					editor.putBoolean("isCurlNeedAssert", false);
					break;
				case 3:
					editor.putBoolean("isChangeSequenceNeedAssert", false);
					break;
				case 4:
					editor.putBoolean("isSubmitSuccessNeedAssert", false);
					break;
				default:
					break;
				}
				editor.commit();
				getFragmentManager().beginTransaction()
						.remove(AssertFragment.this).commit();
			}
		});
		return view;
	}

}
